﻿<cfsilent>
	<cfsetting requesttimeout="900">

	<cfset variables.iterations = 5000>
	<cfquery name="variables.GetItemTypes" datasource="#application.DSN#">
		select id, type
		from itemtypes
		where IsMagic = 1
		order by type
	</cfquery>
</cfsilent><cfmodule template="../_header.cfm">
</cfmodule>

<form id="testForm" method="post">
	<select name="itemtypeid" id="itemTypeId">
		<option value="1">minor</option>
		<option value="2">medium</option>
		<option value="3">major</option>
	</select>
	<br/>
	<select name="ItemType" id="itemType">
		<cfoutput query="variables.GetItemTypes">
			<option value="#variables.GetItemTypes.ID#">#variables.GetItemTypes.Type#</option>
		</cfoutput>
	</select>
	<br/>
	<input type="text" name="iterations" id="iterations" value="<cfoutput>#variables.iterations#</cfoutput>" size="8" />
	<br/>
	<button id="getIt">get it</button>
</form>

<!---<table id="results">
	<thead>
		<tr>
			<th>Item</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>Iterations</td>
		</tr>
	</tbody>
</table>--->

<table id="summary">
	<thead>
		<tr>
			<th>Item</th>
			<th>QTY</th>
			<th>%</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>Iterations</td>
			<td>0</td>
		</tr>
	</tbody>
</table>

<cfmodule template="../_footer.cfm">
	<script type="text/javascript">
		$(document).ready( function() {
			$('#getIt').button()
			.click( function(e) {
				e.preventDefault();

				// post test form to tests.cfc::GetEnhancementBonus
				var $form = $('#testForm');
				var data = {
					method: 'GetMagicItem',
					itemCategoryID: $form.find($('#itemTypeId')).val(),
					iterations: $form.find($('#iterations')).val(),
					itemTypeID: $form.find($('#itemType')).val()
				};
				var iterations = data.iterations;
				$.ajax({
					data: data,
					success: function(data) {
						var data = eval("(" + data + ")");
						$('#summary tbody tr:last td:eq(1)').html(parseInt($('#summary tbody tr:last td:eq(1)').html()) + parseInt(iterations));

						$('#results tbody tr:last').append('<td>' + parseInt(iterations) + '</td><td></td>');
						$('#results thead tr').append('<th>QTY</th><th>%</th>');
						$.each(data, function(index, value) {
							// find index in a cell
							var $findThis = $('#results tbody tr').filter( function(idx){
								// find an exact match for ability type
								if ($(this).data('type') == index){
									return this;
								}
							});

							if (!$findThis.length){
								// add a new row
								var $findThis = $('#results tbody tr:last').before('<tr><td>' + index + '</td></tr>').prev().data('type', index);
							}

							for (var i = $findThis.find('td').length; i < $('#results thead th').length - 2; i++){
								// add empty cells to align table columns
								$findThis.append('<td></td>');
							}

							// add item to row
							$findThis.append('<td>' + parseInt(value) + '</td><td>' + Math.round(value/iterations * 100 * 100) /100 + '</td>');

							// add data to summary table
							var $findSummary = $('#summary tbody tr').filter( function(idx){
								// find an exact match for ability type
								if ($(this).data('summaryType') == index){
									return this;
								}
							});

							if (!$findSummary.length){
								// add a new row
								$findSummary = $('#summary tbody tr:last').before('<tr><td>' + index + '</td><td>' + parseInt(value) + '</td><td>' + Math.round(value/parseInt($('#summary tbody tr:last td:eq(1)').html()) * 100 * 100) / 100 + '</td></tr>').prev().data('summaryType', index);
							} else {
								$findSummary.find('td:eq(1)').html(parseInt($findSummary.find('td:eq(1)').html()) + parseInt(value));
								$findSummary.find('td:eq(2)').html(Math.round(parseInt($findSummary.find('td:eq(1)').html()) / parseInt($('#summary tbody tr:last td:eq(1)').html()) * 100 * 100) / 100);
							}
						});
					}
				});
			});
		});

		$.ajaxSetup({
			data: {
				returnformat: 'json'
			},
			type: 'post',
			url: '/pathfinder/cfcs/tests.cfc'
		});
	</script>
</cfmodule>